package com.klhappy.houyuan.fragement;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

import com.klhappy.houyuan.R;
import com.klhappy.houyuan.fragement.dingdanfragment.EvaluateFragment;
import com.klhappy.houyuan.fragement.dingdanfragment.FinishFragment;
import com.klhappy.houyuan.fragement.dingdanfragment.PayFragment;
import com.klhappy.houyuan.fragement.dingdanfragment.SendFragment;
import com.klhappy.houyuan.view.AbsFragment;

public class DingDan_Fragment extends AbsFragment implements View.OnClickListener{
    private static final String[] TAB_CONTENT = new String[]{ "待付款", "待派送", "待评价", "已完成"};

    @InjectView(R.id.fu_fs_tv) TextView fu_fs_tv;
    @InjectView(R.id.fu_fs_v) View fu_fs_v;
    @InjectView(R.id.ps_fs_tv) TextView ps_fs_tv;
    @InjectView(R.id.ps_fs_v) View ps_fs_v;
    @InjectView(R.id.pj_fs_tv) TextView pj_fs_tv;
    @InjectView(R.id.pj_fs_v) View pj_fs_v;
    @InjectView(R.id.tk_fs_tv) TextView tk_fs_tv;
    @InjectView(R.id.tk_fs_v) View tk_fs_v;
    @InjectView(R.id.pager) ViewPager pager;

    private int black = 0xFF535353;
    private int green = 0xFF869e34;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg3, container, false);
        ButterKnife.inject(this, view);

        setOnClickListener();
        List<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(new PayFragment());
        fragments.add(new SendFragment());
        fragments.add(new EvaluateFragment());
        fragments.add(new FinishFragment());
        pager.setOffscreenPageLimit(TAB_CONTENT.length);
        pager.setAdapter(new MainActivityAdapter(getActivity().getSupportFragmentManager(), fragments));
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position == 0){
                    setChioceItem(0);
                }else if(position == 1){
                    setChioceItem(1);
                }else if(position == 2){
                    setChioceItem(2);
                }else if(position == 3){
                    setChioceItem(3);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        return view;
    }

    private void setOnClickListener(){
        fu_fs_tv.setOnClickListener(this);
        ps_fs_tv.setOnClickListener(this);
        pj_fs_tv.setOnClickListener(this);
        tk_fs_tv.setOnClickListener(this);
    }

    class MainActivityAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragments;

        public MainActivityAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.mFragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position % mFragments.size());
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TAB_CONTENT[position % mFragments.size()];
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fu_fs_tv:
                setChioceItem(0);
                break;
            case R.id.ps_fs_tv:
                setChioceItem(1);
                break;
            case R.id.pj_fs_tv:
                setChioceItem(2);
                break;
            case R.id.tk_fs_tv:
                setChioceItem(3);
                break;
            default:
                break;
        }
    }

    private void setChioceItem(int type){
        setView();
        switch (type){
            case 0:
                fu_fs_tv.setTextColor(green);
                fu_fs_v.setVisibility(View.VISIBLE);
                pager.setCurrentItem(0);
                break;
            case 1:
                ps_fs_tv.setTextColor(green);
                ps_fs_v.setVisibility(View.VISIBLE);
                pager.setCurrentItem(1);
                break;
            case 2:
                pj_fs_tv.setTextColor(green);
                pj_fs_v.setVisibility(View.VISIBLE);
                pager.setCurrentItem(2);
                break;
            case 3:
                tk_fs_tv.setTextColor(green);
                tk_fs_v.setVisibility(View.VISIBLE);
                pager.setCurrentItem(3);
                break;
            default:
                break;
        }
    }

    private void setView(){
        fu_fs_tv.setTextColor(black);
        fu_fs_v.setVisibility(View.GONE);
        ps_fs_tv.setTextColor(black);
        ps_fs_v.setVisibility(View.GONE);
        pj_fs_tv.setTextColor(black);
        pj_fs_v.setVisibility(View.GONE);
        tk_fs_tv.setTextColor(black);
        tk_fs_v.setVisibility(View.GONE);
    }
}
